package com.lxyk.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lxyk.yygh.cmn.client.DictFeignClient;
import com.lxyk.yygh.enums.DictEnum;
import com.lxyk.yygh.hosp.repository.HospitalRepository;
import com.lxyk.yygh.hosp.service.HospitalService;
import com.lxyk.yygh.model.hosp.Hospital;
import com.lxyk.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    //上传医院信息
    @Override
    public void save(Map<String, Object> switchMap) {
        //map转Hospital对象
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(switchMap), Hospital.class);

        String hoscode = hospital.getHoscode();
        //判断是否存在
        Hospital hospital1Exist = hospitalRepository.getHospitalByHoscode(hoscode);

        //如果存在  进行修改
        if (null != hospital1Exist){
            hospital.setStatus(hospital1Exist.getStatus());
            hospital.setCreateTime(hospital1Exist.getCreateTime());
            hospital.setUpdateTime(hospital1Exist.getUpdateTime());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else {
            //不存在  添加    0 未上线   1  上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    //查询医院
    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    //医院列表
    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Pageable pageable = PageRequest.of(page - 1, limit);

        //创建匹配器
        ExampleMatcher matcher=ExampleMatcher
                .matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> all = hospitalRepository.findAll(example, pageable);
        //getContent先获取list集合,然后遍历进行封装
        all.getContent().stream().forEach(item->{
            this.packHospital(item);
        });
        return all;
    }

    //医院上线状态
    @Override
    public void updateStatus(String id, Integer status) {
        if(status.intValue()==0||status.intValue()==1){
            Hospital hospital = hospitalRepository.findById(id).get();
            //设置修改的值
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    //获取医院详情
    @Override
    public Map<String, Object> getHospById(String id) {
        Map<String,Object> result=new HashMap<>();
        Hospital hospital = this.packHospital(hospitalRepository.findById(id).get());
        result.put("hospital",hospital);
        result.put("bookingRule",hospital.getBookingRule());//处理规则
        hospital.setBookingRule(null);
        return result;
    }

    //获取医院名称
    @Override
    public String getByHosName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(null!=hospital){
            return hospital.getHosname();
        }
        return hoscode;
    }

    //根据医院名称查询
    @Override
    public List<Hospital> findByHosName(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    //查询医院规则
    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String,Object> result = new HashMap<>();

        Hospital hospital = this.packHospital(this.getByHoscode(hoscode));
        result.put("hospital",hospital);
        result.put("bookingRule",hospital.getBookingRule());

        hospital.setBookingRule(null);
        return result;
    }

    //医院等级封装
    private Hospital packHospital(Hospital hospital){
        //根据 dictcode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hospital.getHostype());

        //查询 省 市 地区
        String provinceCode = dictFeignClient.getName(hospital.getProvinceCode());
        String cityCode = dictFeignClient.getName(hospital.getCityCode());
        String districtCode = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("hostypeString",hostypeString);
        hospital.getParam().put("fullAddress",provinceCode+cityCode+districtCode+hospital.getAddress());
        return hospital;
    }
}
